
// manualCalibView.cpp : implementation of the CmanualCalibView class
//

#include "stdafx.h"
#include "manualCalib.h"

#include "manualCalibDoc.h"
#include "manualCalibView.h"
#include "cvvImage.h"
#include "VideoConvert.h"
#include "VideoGrab.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CmanualCalibView

IMPLEMENT_DYNCREATE(CmanualCalibView, CScrollView)

BEGIN_MESSAGE_MAP(CmanualCalibView, CScrollView)
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, &CScrollView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, &CScrollView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CmanualCalibView::OnFilePrintPreview)
	ON_WM_LBUTTONDOWN()
	//ON_COMMAND(ID_LOAD_EXTRACTGRIDCORNERS, &CmanualCalibView::OnLoadExtractgridcorners)
//	ON_COMMAND(ID_LOAD_ADDTIONALIMAGES, &CmanualCalibView::OnLoadAddtionalimages)
//	ON_COMMAND(ID_LOAD_IMAGES, &CmanualCalibView::OnLoadImages)
	ON_COMMAND(ID_VIDEO_CONVERT, &CmanualCalibView::OnVideoConvert)
	ON_COMMAND(ID_VIDEO_GRAB, &CmanualCalibView::OnVideoGrab)
	ON_COMMAND(ID_IMAGES_DOUBLE, &CmanualCalibView::OnImagesDouble)
END_MESSAGE_MAP()

// CmanualCalibView construction/destruction

CmanualCalibView::CmanualCalibView()
: m_grabList(NULL)
{
	// TODO: add construction code here

}

CmanualCalibView::~CmanualCalibView()
{
}

// CCalibData *CmanualCalibView::CreateCaliData(void)
// {
// 	//from single image
// 	if(m_pTemplateImage)
// 	{
// 		CCalibData* pCalibData = new CCalibData;
// 		pCalibData->addImage(m_pTemplateImage, NULL);
// 		return pCalibData;
// 	}
// 	
// 	//from multiple image names
// 	POSITION iPos = m_fileNameList.GetHeadPosition();
// 	if (iPos)
// 	{
// 		const char *fileName = m_fileNameList.GetNext(iPos).GetBuffer(0);
// 		m_pTemplateImage = cvLoadImage(fileName, 1);
// 		
// 		CCalibData* pCalibData= NULL;
// 		if(m_pTemplateImage)
// 		{
// 			pCalibData = new CCalibData;
// 			pCalibData->addImage(m_pTemplateImage, NULL);
// 		}
// 		else
// 		{
// 			AfxMessageBox(_T("Bad file name!"), MB_OK);
// 		}
// 		m_fileNameList.RemoveHead();
// 		return pCalibData;		
// 	}
// 
// 	return NULL;
// }

// void CmanualCalibView::OnLoadAddtionalimages()
// {
// 	// TODO: Add your command handler code here
// 
// 	// load single file from a dialog
// 	CFileDialog iFileDlg(true);
// 	string strFile;
// 	
// 	if (iFileDlg.DoModal()==IDOK)
// 	{
// 		m_pTemplateImage = NULL;
// 		strFile=iFileDlg.GetPathName();
// 		m_pTemplateImage = cvLoadImage(strFile.c_str(), 1);
// 		if (m_pTemplateImage)
// 		{
// 			m_pTemplateCalibData = CreateCaliData();
// 			m_ExtractCornerMode = TRUE;
// 			InvalidateRect(0, FALSE);
// 		}		
// 	}
// }

// void CmanualCalibView::OnLoadImages()
// {
// 	// TODO: Add your command handler code here
// 
// 	CFileDialog dlgFile(TRUE);
// 	CString fileName;
// 	const int c_cMaxFiles = 50;
// 	const int c_cbBuffSize = (c_cMaxFiles * (MAX_PATH + 1)) + 1;
// 	dlgFile.GetOFN().lpstrFile = fileName.GetBuffer(c_cbBuffSize);
// 	dlgFile.GetOFN().nMaxFile = c_cbBuffSize;
// 	dlgFile.GetOFN().Flags = dlgFile.GetOFN().Flags | OFN_ALLOWMULTISELECT ;
// 	
// 	m_fileNameList.RemoveAll();
// 	//record file name
// 	if (dlgFile.DoModal() == IDOK)
// 	{
// 		POSITION pos = dlgFile.GetStartPosition();
// 		while(pos)
// 		{
// 			m_fileNameList.AddTail(dlgFile.GetNextPathName(pos));
// 		}
// 
// 	}
// 	fileName.ReleaseBuffer();
// 
// 	//read first name
// 	m_pTemplateCalibData = CreateCaliData();
// 	if (m_pTemplateCalibData)
// 	{
// 		m_ExtractCornerMode = TRUE;
// 		InvalidateRect(0, FALSE);
// 	}
// }

BOOL CmanualCalibView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}


void CmanualCalibView::OnDraw(CDC* pDC)
{
	CmanualCalibDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;

	// TODO: add draw code for native data here

	//in extract corner mode, show the original image and corners
// 	if (m_pTemplateCalibData && m_ExtractCornerMode)
// 	{
// 		IplImage *aImage = NULL;
// 		aImage = m_pTemplateCalibData->GetImage();
// 		if (aImage!= NULL)
// 		{
// 			CClientDC aDc(this);
// 			OnPrepareDC(&aDc);
// 
// 			CmfcImage image;
// 			image.passImage(aImage);
// 			CRect r;
// 			GetClientRect (&r);
// 			r.right = aImage->width + r.left;
// 			r.bottom = aImage->height + r.top;
// 			image.DrawToHDC(pDC->GetSafeHdc(), r);
// 		}
// 		m_pTemplateCalibData->Draw(pDC, 1.0);
// 	}	
}


// CmanualCalibView printing


void CmanualCalibView::OnFilePrintPreview()
{
	AFXPrintPreview(this);
}

BOOL CmanualCalibView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CmanualCalibView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CmanualCalibView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

void CmanualCalibView::OnRButtonUp(UINT nFlags, CPoint point)
{
	ClientToScreen(&point);
	OnContextMenu(this, point);
}

void CmanualCalibView::OnContextMenu(CWnd* pWnd, CPoint point)
{
	theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
}


// CmanualCalibView diagnostics

#ifdef _DEBUG
void CmanualCalibView::AssertValid() const
{
	CView::AssertValid();
}

void CmanualCalibView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CmanualCalibDoc* CmanualCalibView::GetDocument() const // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CmanualCalibDoc)));
	return (CmanualCalibDoc*)m_pDocument;
}
#endif //_DEBUG


// CmanualCalibView message handlers


void CmanualCalibView::OnInitialUpdate()
{
	CScrollView::OnInitialUpdate();

	// TODO: Add your specialized code here and/or call the base class
	CSize DocSize(400, 300);

	// Set mapping mode and document size
	SetScrollSizes(MM_TEXT, DocSize);
}

// void CmanualCalibView::OnLButtonDown(UINT nFlags, CPoint point)
// {
// 	// TODO: Add your message handler code here and/or call default
// 	CClientDC aDc(this);
// 	OnPrepareDC(&aDc);
// 	aDc.DPtoLP(&point);  //device coordinate to logic coordinate
// 
// 
// 	//extract corners
// 	if(m_ExtractCornerMode && m_pTemplateCalibData)
// 	{
// 		m_pTemplateCalibData->addCorner(Point2f((float)point.x, (float)point.y));
// 		InvalidateRect(0, FALSE);
// 		//finish add corners
// 		if (m_pTemplateCalibData->finishAddCorner())
// 		{
// 			int isOk = AfxMessageBox(_T("Accept Marked Corners ?"), MB_OKCANCEL);
// 			if (isOk == IDOK)
// 			{
// 				GetDocument()->AddCalibData(m_pTemplateCalibData);
// 				m_pTemplateCalibData = NULL;
// 				m_pTemplateImage = NULL;
// 				m_ExtractCornerMode = FALSE;
// 				
// 				//start another extract if there is other image in the filelist
// 				if(!m_fileNameList.IsEmpty())
// 				{
// 					m_pTemplateCalibData = CreateCaliData();
// 					m_ExtractCornerMode  = TRUE;
// 				}
// 			}
// 			else if (isOk == IDCANCEL)
// 			{
// 				m_pTemplateCalibData->deleteAllCorners();
// 			}
// 			InvalidateRect(0, FALSE);
// 		}		
// 	}
// 	CScrollView::OnLButtonDown(nFlags, point);
// }

// void CmanualCalibView::OnLoadExtractgridcorners()
// {
// 	// TODO: Add your command handler code here
// 
// 	//get client DC
// 	CClientDC aDc(this);
// 	OnPrepareDC(&aDc);
// 	m_ExtractCornerMode = true;
// 
// 	//get doc
// 	CmanualCalibDoc* pDoc = GetDocument();
// 	ASSERT_VALID(pDoc);
// 	if (!pDoc)
// 		return;
// 
// 	InvalidateRect(0);
// }





void CmanualCalibView::OnVideoConvert()
{
	// TODO: Add your command handler code here

	//load, change and save video format, resolution, and start end time code
	//load left video name
	int leftOk = AfxMessageBox(_T("Load Left Video File"), MB_OK);
	string strLeftFile;
	string strRightFile;
	if (leftOk == IDOK)
	{
		CFileDialog iFileDlg(true);
		if (iFileDlg.DoModal()==IDOK)
		{
			strLeftFile=iFileDlg.GetPathName();			
		}
	}
	//load right video name
	int rightOk = AfxMessageBox(_T("Load Right Video File"), MB_OK);
	if (rightOk == IDOK)
	{
		CFileDialog iFileDlg(true);
		if (iFileDlg.DoModal()==IDOK)
		{
			strRightFile=iFileDlg.GetPathName();			
		}
	}
	if (!strLeftFile.empty() && !strRightFile.empty())
	{
		SIZE leftSize, rightSize;

		bool openVideo = GetDocument()->ConvertVideo(strLeftFile, strRightFile, leftSize, rightSize);
		if (openVideo)
		{
			CVideoConvert iVC;
			iVC.SetConvert(strLeftFile, strRightFile, leftSize, rightSize);
			if (iVC.DoModal()==IDOK)
			{
			}
		}

	}
	//@todo show convert result

}

void CmanualCalibView::OnVideoGrab()
{
	// TODO: Add your command handler code here
	//@todo
#if 1
	int leftOk = AfxMessageBox(_T("Load Left Video File"), MB_OK);
	string strLeftVideo;
	string strRightVideo;
	if (leftOk == IDOK)
	{
		CFileDialog iFileDlg(true);
		if (iFileDlg.DoModal()==IDOK)
		{
			strLeftVideo=iFileDlg.GetPathName();			
		}
	}
	//load right video name
	int rightOk = AfxMessageBox(_T("Load Right Video File"), MB_OK);
	if (rightOk == IDOK)
	{
		CFileDialog iFileDlg(true);
		if (iFileDlg.DoModal()==IDOK)
		{
			strRightVideo = iFileDlg.GetPathName();			
		}
	}
#endif

	//@todo
 //	string strLeftVideo = string("video\\calib_zoom90\\left_top_1920.avi");
 //	string strRightVideo = string("video\\calib_zoom90\\right_bot_1920.avi");
// 
 	CVideoGrab iGrabDlg;
 	bool fileOk = iGrabDlg.SetVideo(strLeftVideo, strRightVideo);
 	if (fileOk && iGrabDlg.DoModal() == IDOK)
 	{
 
 	}

}

void CmanualCalibView::OnImagesDouble()
{
	// TODO: Add your command handler code here
	if (m_grabList)
	{
		m_grabList->ShowWindow(SW_SHOW);
		m_grabList->SetForegroundWindow();
	}
	else
	{
		m_grabList = new CGrabList(this);
		m_grabList->Create(CGrabList::IDD, this);
		m_grabList->ShowWindow(SW_SHOW);
		m_grabList->SetForegroundWindow();
	}
	
}

